<template>
  <div>
    <BasicModal v-bind="$attrs" @register="register" showFooter :title="getTitle" width="50%" @ok="handleSubmit"
      :loading="loadding">
      发货成功之后前往微信打印订单软件进行打印快递单号
    </BasicModal>
  </div>

</template>
<script lang="ts" setup>
import { ref, computed, unref } from 'vue';
import { BasicForm, useForm } from '@/components/Form';
import { sendGoodFormSchema } from './role.data';
import { BasicModal, useModalInner } from '@/components/Modal';
import { BasicTree, TreeItem } from '@/components/Tree';
import { sendOutGoodsApi } from '@/api/sys/sysOrder';

import { useMessage } from '@/hooks/web/useMessage';

const emit = defineEmits(['success', 'register']);
const isUpdate = ref(true);
const { createMessage } = useMessage();
const loadding = ref(false);
const orderId = ref('');

const [register, { setModalProps, closeModal }] = useModalInner(async (data) => {
  //打开弹层后的回调
  setModalProps({ confirmLoading: false });
  // 需要在setFieldsValue之前先填充treeData，否则Tree组件可能会报key not exist警告
  // if (unref(treeData).length === 0) {
  //   treeData.value = (await getMenuList()) as any as TreeItem[];
  // }
  isUpdate.value = !!data?.isUpdate;
  orderId.value = data.record.orderId;
  console.log('---data.record:', orderId.value);

});

const getTitle = computed(() => ("确认进行发货操作？"));

async function handleSubmit() {
  try {
    // const values = await validate();
    let values = { orderId: "" };
    loadding.value = true;
    setModalProps({ confirmLoading: true });
    // TODO custom api
    values.orderId = orderId.value;

    //发货
    sendOutGoodsApi(values)
      .then((res) => {
        // console.log("---res::",res)
        if (res.code == 200) {
          createMessage.success(res.message);
        } else {
          createMessage.error(res.message);
        }
      })
      .catch((res) => {
        createMessage.error(res.message);
      })
      .finally(() => {
        loadding.value = false;

        closeModal();
        emit('success');
      });
  } finally {
    setModalProps({ confirmLoading: false });
  }
}
</script>
